<?php
// 测试系统配置API
require_once __DIR__ . '/vendor/autoload.php';

// 测试API
$baseUrl = 'http://localhost:8000/api';

// 先登录获取Token
echo "登录获取Token...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/auth/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'username' => 'admin',
    'password' => '123456'
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "登录HTTP状态码: " . $httpCode . "\n";
echo "登录响应: " . $response . "\n\n";

$loginData = json_decode($response, true);
if ($loginData['code'] !== 200) {
    echo "登录失败，退出测试\n";
    exit(1);
}

$token = $loginData['data']['token'];
echo "获取到Token: " . $token . "\n\n";

// 测试获取外部API配置
echo "测试获取外部API配置...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/system-configs/external-api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $token,
    'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP状态码: " . $httpCode . "\n";
echo "响应: " . $response . "\n\n";

// 测试Token测试接口
echo "测试Token测试接口...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/system-configs/test-token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $token,
    'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP状态码: " . $httpCode . "\n";
echo "响应: " . $response . "\n\n";

// 测试更新外部API配置
echo "测试更新外部API配置...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/system-configs/external-api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'xiaofeng_order_token' => 'test_order_token',
    'xiaofeng_create_order_token' => 'test_create_token'
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $token,
    'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP状态码: " . $httpCode . "\n";
echo "响应: " . $response . "\n\n"; 